﻿var allcontextmenus = new Array();
var currentMenu;

function contextMenu(props, events) {
    this.props = props;
    this.events = events;
    this.menuObj = document.getElementById(this.props.menuID);
    this.targetEle = this.props.targetEle;
    //this.eventFunc = this.events;
    allcontextmenus.push(this);
}

contextMenu.prototype.buildContextMenu = function() {
    //    var menuObj = document.getElementById(this.props.menuID);
    //    var targetEle = this.props.targetEle;
    var eventFunc = this.events;

    document.oncontextmenu = function(evt) {
        try {
            hideHotKeySelect(); //隐藏发送消息热键选择Div
            clearEventBubble(evt);
            var cobj = ele(evt);
            if (currentMenu == null || currentMenu.currentObj != cobj) {
                currentMenu = existsMenuClass(cobj.className);
            }
            if (currentMenu != null) {
                eventFunc = currentMenu.events;
                hide();
                if (currentMenu.currentObj != null) {
                    currentMenu.currentObj.className = currentMenu.targetEle;
                }
                currentMenu.currentObj = cobj;

                cobj.className = 'MouseOver';


                var _items = currentMenu.menuObj.getElementsByTagName("li");
                for (var i = 0; i < _items.length; i++) {
                    if (_items[i].className != "separator" && containsItem(_items[i].id)) {
                        _items[i].className = "item";
                        _items[i].onmouseout = function() { this.className = "item"; } // if (this.id == "edit") { this.getElementsByTagName('ul')[0].style.display = "none"; } };
                        _items[i].onclick = function() { hide(); func(this.id, cobj); };
                        _items[i].onmouseover = function() { this.className = "item itemOver"; } // if (this.id == "edit") { this.getElementsByTagName('ul')[0].style.display = "block"; } };
                        //modified by dingyuliang 2009-06-29
                        if (_items[i].id == "edit") {
                            var result = chatblog.usercate.chatroom.GetGroupListUlHtml(cobj.getElementsByTagName("input")[0].value, cobj.id);
                            _items[i].getElementsByTagName("ul")[0].innerHTML = result.value;
                        }
                    }
                }

                if (cobj.tagName == "H2" && (cobj.childNodes[0].nodeValue == "默认分组"||cobj.childNodes[0].nodeValue == "陌生人")) {
                    for (var i = 0; i < _items.length; i++)
                        if (_items[i].id == "renameGroup" || _items[i].id == "deleteGroup") {
                        _items[i].style.display = "none";
                    }
                }
                else {
                    for (var i = 0; i < _items.length; i++)
                        if (_items[i].id == "renameGroup" || _items[i].id == "deleteGroup") {
                        _items[i].style.display = "block";
                    }
                }

                var _bodyWidth = document.body.offsetWidth || document.body.clientWidth;
                var _bodyHeight = document.body.offsetHeight || document.body.clientHeight;
                var _mWidth = currentMenu.menuObj.style.width;
                var _mHeight = currentMenu.menuObj.offsetHeight;

                currentMenu.menuObj.style.left = ((parseInt(getX(evt)) + parseInt(_mWidth)) > parseInt(_bodyWidth) ? (parseInt(getX(evt)) - parseInt(_mWidth)) : getX(evt)) + "px";
                currentMenu.menuObj.style.top = ((parseInt(getY(evt)) + parseInt(_mHeight)) > parseInt(_bodyHeight) ? (parseInt(getY(evt)) - parseInt(_mHeight)) : getY(evt)) + "px";
                currentMenu.menuObj.style.display = "block";


            } else {
                hide();
            }

        } catch (e) {
        } finally {
            clearEventBubble(evt);
            return false;
        }

    }

    document.onclick = function(thisObj) {
        hide();
        if (currentMenu != null && currentMenu.currentObj != null) { currentMenu.currentObj.className = currentMenu.targetEle; currentMenu.currentObj = null; }
        thisObj = thisObj || window.event;
        var thisObjEle = ele(thisObj);
        if (thisObjEle != $('hotKeysButton')) {
            hideHotKeySelect(); //隐藏发送消息热键选择Div
        }

    }

    func = function(fid, srcEle) {
        eventFunc.bindings[fid](srcEle);
    }

    hide = function() {
        try {
            for (i = 0; i < allcontextmenus.length; i++) {
                allcontextmenus[i].menuObj.style.display = "none";
                if (allcontextmenus[i].currentObj != null)
                    allcontextmenus[i].currentObj.className = allcontextmenus[i].targetEle;
            }
        } catch (e) { }
    }
    ele = function(evt) {
        evt = evt || window.event;
        return (evt.srcElement || evt.target);
    }
}

existsMenuClass = function(cssName) {
    for (i = 0; i < allcontextmenus.length; i++) {
        if (allcontextmenus[i].props.targetEle == cssName)
            return allcontextmenus[i];
    }
    return null;
}

containsItem = function(itemId) {
var itemidList = ['addGroup', 'add', 'addUser', 'deleteGroup', 'renameGroup', 'del', 'edit', 'prop', 'rename'];
    for (var i = 0; i < itemidList.length;i++ ) {
        if (itemidList [i] == itemId)
            return true;
    }
    return false;
}
/*==============================================================*/
function isIE(){ //ie? 
if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
    return true; 
else 
    return false; 
} 

if(!isIE()){ //firefox innerText define
    HTMLElement.prototype.__defineGetter__("innerText", 
    function(){
        var anyString = "";
        var childS = this.childNodes;
        for(var i=0; i<childS.length; i++) { 

            if(childS[i].nodeType==1)
                //anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;
                anyString += childS[i].innerText;
            else if(childS[i].nodeType==3)
                anyString += childS[i].nodeValue;
        }
        return anyString;
    }
    );    

    HTMLElement.prototype.__defineSetter__("innerText",
    function(sText) {
        this.textContent = sText;


    }
    );
    
}

function getX(evt) {
    evt = evt || window.event;
    /**
    CssRain---增加代码. 8月20日 
    使右键菜单，在有滚动条的情况下，也能正常在点击出弹出。
    */
    var scrollPosX;
    if (typeof window.pageYOffset != 'undefined') {
        scrollPosX = window.pageXOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        scrollPosX = document.documentElement.scrollLeft;
    }
    else if (typeof document.body != 'undefined') {
        scrollPosX = document.body.scrollLeft;
    }        
    return (evt.x || evt.clientX || evt.pageX) + scrollPosX;
}

function getY(evt) {
    evt = evt || window.event;
    /**
    CssRain---增加代码. 8月20日 
    使右键菜单，在有滚动条的情况下，也能正常在点击出弹出。
    */
    var scrollPosY;
    if (typeof window.pageYOffset != 'undefined') {
        scrollPosY = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        scrollPosY = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined') {
        scrollPosY = document.body.scrollTop;
    }

    return (evt.y || evt.clientY || evt.pageY) + scrollPosY;
}

function clearEventBubble(evt) {
    evt = evt || window.event;

    if (evt.stopPropagation) {
        evt.stopPropagation();
    } else {
        evt.cancelBubble = true;
    }

    if (evt.preventDefault) {
        evt.preventDefault();
    } else {
        evt.returnValue = false;
    }

} 
